package com.greenantmall.bean;

import java.text.DecimalFormat;

import com.avos.avoscloud.AVACL;
import com.avos.avoscloud.AVClassName;
import com.avos.avoscloud.AVRole;
import com.greenantmall.utils.iLog;

import android.text.TextUtils;

@AVClassName(AntAVObject.CLASS_AVGOODS)
public class Goods extends AntAVObject
{
    private static final String TAG="Goods";
	
    public static final String ATTR_STOREID= "userId";
    public static final String ATTR_DESCRIPTION = "description";
    public static final String ATTR_SPECS = "specs";
    public static final String ATTR_UNIT = "unit";
    // 标签显示标志：0-无标签，1-促销 2-热卖 3-售完
    public static final String ATTR_SHOW_TAG = "showTag";
    public static final String ATTR_DEDUCTPERCENT = "deductPercent";// 提成百分比

    // 分类
    public static final String ATTR_KIND = "kind";
    public static final String ATTR_STYLE = "style";
    public static final String ATTR_IMAGE_URL = "image";
    public static final String ATTR_DISCOUNT = "discount";// 折扣
    public static final String ATTR_PRICE = "price";
    public static final String ATTR_ORIGINALPRICE = "originalPrice";
    //是否上架，默认为上架
    public static final String ATTR_IS_ON_SHELF = "isOnShelf";
    // 销量
    public static final String ATTR_TOTAL_SALES = "totalSales";
    // 库存
    public static final String ATTR_STOCK = "stock";

    public static final String ATTR_UPTOWNS = "uptowns";//服务区

    //每次更改增加的量
    public static final String ATTR_WEIGHSTEP = "weighStep";
    //每次更改增加的量的单位
    public static final String ATTR_WEIGHUNIT = "weighUnit";
    //是否称重计价
    public static final String ATTR_WEIGHABLE = "weighable";
    public static final String ATTR_ISRECOMMEND = "isRecommend";
    
//    private static final String 

    private AVRole customRole;

    public Goods()
    {
        super();
        setAccessCtrl();
    }

    public Goods(String str)
    {
        super(str);
        setAccessCtrl();
    }

    public int getDeductPercent()
    {
        return this.getInt(ATTR_DEDUCTPERCENT);
    }

    public void setDeductPercent(int descrip)
    {
        this.put(ATTR_DEDUCTPERCENT, descrip);
    }
    
    public double getDiscount()
    {
        return this.getDouble(ATTR_DISCOUNT);
    }

    public void setDiscount(double descrip)
    {
        this.put(ATTR_DISCOUNT, descrip);
    }
    
    
    public String getImageUrl()
    {
        String url="";
        try {
            url=this.getAVFile(ATTR_IMAGE_URL).getUrl();
		} catch (Exception e) {
			iLog.e(TAG, "getImageUrl is null");
		}
        if(TextUtils.isEmpty(url))
        {
            url=this.getString(ATTR_IMAGE_URL);  
        }
        return url;
    }
    
    public void setImageUrl(String descrip)
    {
        this.put(ATTR_IMAGE_URL, descrip);
    }

    public String getDescriptionAndSpecs()
    {
    	if(!TextUtils.isEmpty(getSpecs())) {
			return this.getString(ATTR_DESCRIPTION) + "(" + getSpecs() + ")";
		}
        return this.getString(ATTR_DESCRIPTION);
    }
    
    public String getDescription()
    {
        return this.getString(ATTR_DESCRIPTION);
    }

    public void setDescription(String descrip)
    {
        this.put(ATTR_DESCRIPTION, descrip);
    }
    
    public String getSpecs()
    {
        return this.getString(ATTR_SPECS);
    }

    public void setSpecs(String specs)
    {
        this.put(ATTR_SPECS, specs);
    }

    public void setUnit(String descrip)
    {
        this.put(ATTR_UNIT, descrip);
    }
    public String getUnit()
    {
        return this.getString(ATTR_UNIT);
    }
    
    public void setOriginalPrice(String unit)
    {
        this.put(ATTR_ORIGINALPRICE, unit);
    }

    public double getOriginalPrice()
    {
       return  this.getDouble(ATTR_ORIGINALPRICE);
    }

    public void setPrice(String unit)
    {
        this.put(ATTR_PRICE, unit);
    }

    public double getPrice()
    {
       return  this.getDouble(ATTR_PRICE);
    }
    
    public double getRealPrice()
    {
        DecimalFormat df = new DecimalFormat("#########.##");
        if (this.getDiscount() <= 0)
        {
            return getPrice();
        } else
        {
            return Double.parseDouble(df.format((getPrice() * getDiscount() / 10)));
        }
    }
    
    public void setWeighStep(double unit)
    {
        this.put(ATTR_WEIGHSTEP, unit);
    }

    public double getWeighStep()
    {
    	if(!this.isWeighAble()) {
    		return 1;
    	}
       return  this.getDouble(ATTR_WEIGHSTEP);
    }
    
    public void setWeighUnit(String weighUnit)
    {
        this.put(ATTR_WEIGHUNIT, weighUnit);
    }

	public String getWeighUnit() {
		if (isWeighAble()) {
			return this.getString(ATTR_WEIGHUNIT);
		} else {
			return getUnit();
		}

	}
    
    public void setWeighAble(boolean weighAble) 
    {
    	this.put(ATTR_WEIGHABLE, weighAble);
    }
    
    public boolean isWeighAble() {
    	return this.getBoolean(ATTR_WEIGHABLE);
    }
    
    public void setIsRecommend(boolean isRecommend) {
		this.put(ATTR_ISRECOMMEND, isRecommend);
	}
	
	public boolean isRecommend() {
		return this.getBoolean(ATTR_ISRECOMMEND);
	}

    public void setPrice(double price)
    {
        this.put(ATTR_PRICE, price);
    }

    public double getStock()
    {
        try {
            return this.getDouble(ATTR_STOCK);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public void setStock(double sk)
    {
        this.put(ATTR_STOCK, sk);
    }

    public String getStyle()
    {
        return this.getString(ATTR_STYLE);
    }

    public void setStyle(String style)
    {
        this.put(ATTR_STYLE, style);
    }

    public int getTotalSales()
    {
        return this.getInt(ATTR_TOTAL_SALES);
    }

    public void setTotalSales(int sales)
    {
        this.put(ATTR_TOTAL_SALES, sales);
    }

    public boolean getIsOnShelf()
    {
        return this.getBoolean(ATTR_IS_ON_SHELF);
    }

    public void setShelfState(boolean state)
    {
        this.put(ATTR_IS_ON_SHELF, state);
    }

    public Goods getInstance()
    {
        return this;
    }

    public String getStoreId()
    {
        return this.getString(ATTR_STOREID);
    }

    public void setStoreId(String userId)
    {
        this.put(ATTR_STOREID, userId);
    }

    public int getShowTag()
    {
        return this.getInt(ATTR_SHOW_TAG);
    }
    
    public void setShowTag(int showTag) 
    {
    	this.put(ATTR_SHOW_TAG, showTag);
    }

    public void setShelfState(int tag)
    {
        this.put(ATTR_SHOW_TAG, tag);
    }

    private int setAccessCtrl()
    {

        acl = new AVACL();
        acl.setPublicReadAccess(true);//�˴����õ��������˵Ŀɶ�Ȩ��
        acl.setPublicWriteAccess(false);

        return 0;
    }

}
